﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_AdvSuiteDAL
    {
        public List<WI_AdvSuite> GetAllAdvSuite()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_AdvSuite.AsQueryable();
                List<WI_AdvSuite> adsList = query.ToList();
                return adsList;
            }
        }

        public WI_AdvSuite GetAdvSuite(int suiteId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_AdvSuite suite = ctx.WI_AdvSuite.SingleOrDefault(c => c.SuiteID == suiteId);
                return suite;
            }
        }

        public void CreateAdvSuite(string suiteName)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_AdvSuite wiaSuite = new WI_AdvSuite
                {
                    SuiteName = suiteName
                };
                ctx.WI_AdvSuite.AddObject(wiaSuite);
                ctx.SaveChanges();
            }
        }

        public bool UpdateAdvSuite(int suiteId, string suiteName)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_AdvSuite wiaSuite = ctx.WI_AdvSuite.SingleOrDefault(c => c.SuiteID == suiteId);
                if (wiaSuite != null)
                {
                    wiaSuite.SuiteName = suiteName;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveAdvSuite(int suiteId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_AdvSuite wiaSuite = ctx.WI_AdvSuite.SingleOrDefault(c => c.SuiteID == suiteId);
                if (wiaSuite != null)
                {
                    List<WI_AdvItem> adItemList = ctx.WI_AdvItem.Where(c => c.SuiteID == suiteId).ToList();
                    foreach (var adItem in adItemList)
                    {
                        adItem.SuiteID = null;
                    }
                    ctx.WI_AdvSuite.DeleteObject(wiaSuite);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
